<template>
  <div>
    <div class="page_space">
      <div class="user_info mt-15">
        <p>当前用户：{{ fullName }}</p>
        <p v-if="roleName">角色：{{ roleName }}</p>
      </div>
      <template v-if="roleName !== '采样员'">
        <p class="title_1 base_info">基础信息</p>
        <div
          v-if="
            roleName === '检测员' ||
            roleName === '采样员和检测员' ||
            roleName === ''
          "
          class="card"
          @click="goto('/equipment-info')"
        >
          <div class="info_wrap">
            <div class="info_box">
              <div class="icon">
                <img src="@/assets/img/icon/icon_renyuan.png" alt="" />
              </div>
              <div class="info">
                <p>快速检测设备管理</p>
                <span>快速检测设备</span>
                <span>{{ countOfEquipment }}个</span>
              </div>
            </div>
            <van-icon class="into" name="arrow" />
          </div>
        </div>
        <div
          v-if="roleName === '管理员' || roleName === ''"
          class="card mt-15"
          @click="goto('/person-info')"
        >
          <div class="info_wrap">
            <div class="info_box">
              <div class="icon">
                <img src="@/assets/img/icon/icon_renyuan.png" alt="" />
              </div>
              <div class="info">
                <p>从业人员管理</p>
                <span>从业人员</span>
                <span>{{ countOfPerson }}个</span>
              </div>
            </div>
            <van-icon class="into" name="arrow" />
          </div>
        </div>
      </template>
      <button class="logout btn default" @click="logout">退出登录</button>
    </div>
    <!-- <div class="support">÷
      <p>广东省政务服务数据管理局提供服务</p>
      <p>广州软件应用技术研究院提供技术支持</p>
    </div> -->
    <tab-bar :current="2" />
  </div>
</template>

<script>
import wx from 'weixin-js-sdk'
import tabBar from '@/components/tab-bar'
import {
  getPersonCount,
  getEquipmentCount,
  getUser,
  getRoleName
} from '@/api/person'
export default {
  name: 'me',
  components: {
    tabBar
  },
  data() {
    return {
      countOfPerson: 0,
      countOfEquipment: 0,
      fullName: '',
      roleName: ''
    }
  },
  created() {
    this.getUserInfo()
    this.getCount()
  },
  methods: {
    async getUserInfo() {
      // get fullName
      const { code: code1, data: data1 } = await getUser()
      if (code1 === '0') {
        this.fullName = data1.fullName || ''
        let userId = data1.id
        // get roleName
        const { code: code2, data: data2 } = await getRoleName(userId)
        if (code2 === '0') {
          if (data2.content.length > 0) {
            this.roleName = data2.content[0].roleName || ''
          }
        }
      }
    },
    async getCount() {
      const { code, data: personData } = await getPersonCount()
      if (code === '0') {
        this.countOfPerson = personData.totalElements || 0
      }
      const { code: code2, data: equipmentData } = await getEquipmentCount()
      if (code2 === '0') {
        this.countOfEquipment = equipmentData.totalElements || 0
      }
    },
    // 注销
    logout() {
      this.$dialog
        .alert({
          title: '是否退出登录?',
          showCancelButton: true
        })
        .then(async () => {
          localStorage.removeItem('ecafeToken')
          wx.miniProgram.getEnv(function (res) {
            if (res.miniprogram) {
              wx.miniProgram.reLaunch({
                url: '/pages/login/index?logout=true'
              })
            }
          })
        })
        .catch(() => {
          // on cancel
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.user_info {
  font-size: 14px;
  line-height: 18px;
}
.base_info {
  margin-top: 12px !important;
}
.page_space {
  height: 100vh;
  position: relative;
}
// 退出登录
.logout {
  position: absolute;
  bottom: 140px;
}
.support {
  font-family: PingFangSCMedium-Medium;
  font-size: 13px;
  font-weight: normal;
  line-height: 18px;
  letter-spacing: 0px;
  color: #b2b8bd;
  text-align: center;
  position: absolute;
  left: 0px;
  bottom: 120px;
  width: 100%;
}
.card {
  width: 345px;
  border-radius: 6px;
  background: #fff;
  box-shadow: 0px 2px 8px 0px rgba(178, 184, 189, 0.1);
  padding: 19px 15px;
  .info_wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .info_box {
      display: flex;
      align-items: center;
    }
    .icon {
      width: 32px;
      height: 32px;
      margin-right: 7px;
      opacity: 1;
      background: #fff;
      box-sizing: border-box;
      border: 0.5px solid #7cb1ff;
      backdrop-filter: blur(2px);
      box-shadow: 0px 2px 6px 0px rgba(91, 109, 135, 0.2);
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 20px;
        height: 20px;
      }
    }
    .info {
      p {
        font-size: 14px;
        font-weight: normal;
        line-height: 19px;
        color: #171819;
        margin-bottom: 2px;
      }
      span {
        font-size: 12px;
        font-weight: normal;
        line-height: 16px;
      }
      span:nth-of-type(1) {
        color: #b2b8bd;
        margin-right: 5px;
      }
      span:nth-of-type(2) {
        color: #2163e0;
      }
    }
    .into {
      font-size: 16px;
      color: #b2b8bd;
    }
  }
}
</style>
